package com.mall.service.marketing.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsFlashPromotionProductRelation;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flashProductRelation")
public class SmsFlashPromotionProductRelationController {

    @Autowired
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    /**
     * 分页查询不同场次关联及商品信息
     *
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(@RequestParam(value = "flashPromotionId") Long flashPromotionId,
                             @RequestParam(value = "flashPromotionSessionId") Long flashPromotionSessionId,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageResult pr = smsFlashPromotionProductRelationService.listWithProduct(flashPromotionId, flashPromotionSessionId, pageNum, pageSize);
        return CommonResult.success(pr);
    }

    /**
     * 修改关联信息
     *
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        int count = smsFlashPromotionProductRelationService.update(id, smsFlashPromotionProductRelation);
        if (count > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.success("修改失败");
    }

    /**
     * 删除关联
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        boolean remove = smsFlashPromotionProductRelationService.removeById(id);
        if (remove) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.success("删除失败");
    }

    /**
     * 批量选择商品添加关联
     * @param relationList
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : relationList) {
            smsFlashPromotionProductRelationService.save(smsFlashPromotionProductRelation);
        }
        return CommonResult.success("添加成功");
    }
}
